﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq.Expressions;
using Newtonsoft.Json.Linq;

using CanDoo;
using CanDoo.Core;
using CanDoo.Core.Dto;
using CanDoo.Data;
using CanDoo.Web;
using CanDoo.FineUI;
using CanDoo.Extensions;
using CanDoo.Oil.Core.Models;
using CanDoo.Oil.Core.Contracts;
using CanDoo.Admin.Core.Contracts;
using FineUIPro;

namespace CanDoo.Oil.WebApp.financeSellBill
{
    public partial class Edit : EditPagebase<ISellBillContract, SellBill>
    {
        public ICustomerContract CustomerContract { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FineUIListHelper.BindDDL(typeof(SellBillTypeEnum).ToDescription(), ListItemHelper.EnumToListItems(typeof(SellBillTypeEnum)), 0L, this.ed_SellBillType, true);
                FineUIListHelper.BindDDL(typeof(PayStateEnum).ToDescription(), ListItemHelper.EnumToListItems(typeof(PayStateEnum)), 0L, this.ed_PayState, true);

                //FineUIListHelper.BindDDL<Customer>(CustomerContract.GetResolvedTree(), 0L, ed_Customer,true);
                //FineUIListHelper.BindDDL(CustomerContract.EntityName, this.CustomerContract.GetSelectList(), 0L, ed_Customer,true);
                if (PageMode != pagemode.Add)
                    LoadData();
                else
                {
                    ed_SellBillType.SetValue(0);
                    ed_PayedMoney.SetValue(0);
                    ed_CreateDate.SetValue(System.DateTime.Now.TimeToStringDate());
                    ed_PayState.SetValue(0);
                }
            }
        }

        protected override void LoadData(SellBill entity)
        {
            ed_Name.SetValue(entity.Name);
            ed_Code.SetValue(entity.Code);
            if (entity.Customer!=null)
            {
                ed_Customer.Values=new string[] { entity.Customer.Id.ToString() };
                ed_Customer.Text=entity.Customer.Name;
            }
            ed_SellBillType.SetValue((int)entity.SellBillType);
            ed_Money.SetValue(entity.Money);
            ed_CreateDate.SetValue(entity.CreateDate);
            ed_PayedMoney.SetValue(entity.PayedMoney);
            ed_WaitMoney.SetValue(entity.WaitMoney);
            ed_NeedPayDate.SetValue(entity.NeedPayDate);
            ed_PayState.SetValue((int)entity.PayState);
            ed_Remark.SetValue(entity.Remark);
        }

        protected override OperationResult SaveData(SellBill entity)
        {
            if (PageMode == pagemode.Add)
            {
                entity.Code = Manager.GetNewCode();
            }
            entity.Name = ed_Name.GetValue().ToString();
            entity.Code = ed_Code.GetValue().ToString();
            if (ed_Customer.Values.Count() > 0 && ed_Customer.Values[0].ToLong() >  0L)
                entity.CustomerId = ed_Customer.Values[0].ToLong();
            else
                entity.CustomerId = 0L;

            entity.Money = ed_Money.GetValue().ToDouble();
            entity.CreateDate = ed_CreateDate.GetValue().ToNullableDateTime();
            if(entity.CreateDate ==DateTime.MinValue)
                entity.CreateDate = null;
            entity.PayedMoney = ed_PayedMoney.GetValue().ToDouble();
            entity.WaitMoney = ed_WaitMoney.GetValue().ToDouble();
            entity.NeedPayDate = ed_NeedPayDate.GetValue().ToNullableDateTime();
            if(entity.NeedPayDate ==DateTime.MinValue)
                entity.NeedPayDate = null;

            if (PageMode == pagemode.Add)
            {
                entity.PayState = PayStateEnum.NoPayed;
                entity.SellBillType = SellBillTypeEnum.ByHand;
                entity.PayedMoney = 0;
            }

            entity.Remark = ed_Remark.GetValue().ToString();
            return base.SaveData(entity);
        }

        protected void ed_Money_TextChanged(object sender, EventArgs e)
        {
            var money = ed_Money.GetValue().ToDouble();
            var payedMoney = ed_PayedMoney.GetValue().ToDouble();
            var waitMoney = ed_WaitMoney.GetValue().ToDouble();

            ed_WaitMoney.SetValue(money - payedMoney);
        }
    }
}
